package com.czk.client;

import com.czk.codec.ObjectDecoder;
import com.czk.codec.ObjectEncode;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/4/28 11:09
 */
public class MyClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
/*        pipeline.addLast(new ObjectDecoder(String.class));
        pipeline.addLast(new ObjectEncode(String.class));*/
        // 换行符编码器
        pipeline.addLast(new LineBasedFrameDecoder(1024));
        // 解码转String，注意调整自己的编码格式GBK、UTF-8
        pipeline.addLast(new StringDecoder(Charset.forName("GBK")));
        // 解码转String，注意调整自己的编码格式GBK、UTF-8
        pipeline.addLast(new StringEncoder(Charset.forName("GBK")));
        pipeline.addLast(new NettyClientHandler());
    }
}
